<?php
/**
 * @todo    线上家博会model
 */
namespace Home\Model;
use Think\Model;

class OnlineModel extends Model{
    
    protected $tableName = 'online_one_page';
    
    /**
     * @todo   获取线上家博会活动ID
     * @author fang
     */
    public function getOnlineID(){
        $id = M('config')->where(array('name' => 'ONLINE_ACTIVITY_ID', 'status' => 1))->getField('value');
        return ($id > 0)? $id : 0 ;
    }
    
    /**
     * @todo    获取展馆及相应品类
     * @author  fang
     */
    public function getZoneCategory(){

        // 读取缓存数据
        $cache_data = S('ONLINE_ZONE_CATEGORY');
        if($cache_data != null){
            return $cache_data;
        }
        // 当先活动下的展馆＋品类
        $where['activity_id'] = $this->getOnlineID();
        $zone_category_list = M('online_zone_category')->where($where)->select();
        if($zone_category_list == null){
            return null;
        }
        $zone_ids = array_column($zone_category_list, 'zone_id');
        $category_ids = array_column($zone_category_list, 'online_category_id');

        // 获取活动相关展馆＋品类
        $zone_where['zone_id'] = array('in', $zone_ids);
        $zone_where['status'] = 1;
        $category_where['category_id'] = array('in', $category_ids);
        $category_where['status'] = 1;
        $zone_list = M('online_zone')->field('zone_id,zone_name,zone_title')->where($zone_where)->order('sort desc,zone_id desc')->select();
        $category_list = D('OnlineCategoryView')->where($category_where)->order('sort desc')->select();

        $zone = array();
        foreach ($zone_list as $key=>$val){
            $zone[$val['zone_id']] = $val;
        }
        foreach ($category_list as $key=>$val){
            $zone[$val['zone_id']]['category_list'][] = $val;
        }
        // 数据缓存1小时
        S('ONLINE_ZONE_CATEGORY', $zone, 3600);
        return $zone;
    }
    
    /**
     * @todo    获取活动的展馆＋品类＋店铺数据
     * @author  fang
     */
    public function getZoneCategoryShop(){

        // 读取缓存数据        
        $cache_data = S('ONLINE_ZONE_CATEGORY_SHOP');
        if($cache_data != null){
            return $cache_data;
        }
        // 获取活动的展会＋品类
        $zone_category_list = M('online_zone_category')->where(array('activity_id' => $this->getOnlineID()))->select();
        $zone_ids = array_column($zone_category_list, 'zone_id');
        $category_ids = array_column($zone_category_list, 'online_category_id');
        // 获取展馆
        $zone_where = array(
            'zone_id' => array('in', $zone_ids),
            'status' => 1,
        );
        $zone_list = M('online_zone')->where($zone_where)->getField('zone_id,zone_name');
        // 获取品类
        $category_where = array(
            'category_id' => array('in', $category_ids),
            'status' => 1,
        );
        $category_list = D('OnlineCategoryView')->where($category_where)->select();
        // 获取店铺
        $shop_where = array(
            'activity_id' => $this->getOnlineID(),
            'zone_id' => array('in', $zone_ids),
        );
        $shop_list = M('online_shop')->field('online_shop_id,online_shop_name,online_category_id')->where($shop_where)->select();

        $shop = $category = $zone = array();
        // 店铺数据处理
        foreach ($shop_list as $key=>$val){
            $shop[$val['online_category_id']][] = array(
                'shop_id' => $val['online_shop_id'],
                'shop_name' => $val['online_shop_name'],
                'shop_link' => U('Online/shop' , array('shop_id' => $val['online_shop_id'])),
            );
        }
        // 品类数据处理
        foreach ($category_list as $key=>$val){
            $category[$val['zone_id']][] = array(
                'category_id' => $val['online_category_id'],
                'category_name' => $val['category_name'],
                'category_shop' => (array)$shop[$val['online_category_id']],
            );
        }
        // 展馆数据处理
        foreach ($zone_list as $key=>$val){
            $zone[] = array(
                'zone_id' => $key,
                'zone_name' => $val,
                'zone_category' => (array)$category[$key],
            );
        }

        // 数据缓存1小时
        S('ONLINE_ZONE_CATEGORY_SHOP', $zone, 3600);
        return $zone;
    }
    
    /**
     * @todo    获取线上家博会品类详细信息
     * @author  fang
     */
    public function getCategoryInfo($category_id){
        if($category_id == 0) return null;
        return M('online_zone_category')->field(true)->where(array('activity_id' => $this->getOnlineID(), 'online_category_id' => $category_id))->find();
    }
    
    /**
     * @todo    获取线上家博会店铺信息
     * @author  fang
     */
    public function getShopInfo($shop_id){
        if($shop_id == 0) return null;
        $where = array(
            'activity_id' => $this->getOnlineID(),
            'online_shop_id' => $shop_id,
            'status' => 1,
        );
        return M('online_shop')->field(true)->where($where)->find();
    }
    
    /**
     * @todo    获取线上家博会店铺的商品列表
     * @author  fang
     */
    public function getGoodsList($shop_id){
        if($shop_id == 0) return null;
        $where = array(
            'activity_id' => $this->getOnlineID(),
            'online_shop_id' => $shop_id,
            'status' => 1,
        );
        return M('online_shop_goods')->field(true)->where($where)->select();
    }
    
    /**
     * @todo    获取功能区列表
     * @author  fang
     */
    public function getPageList($where = array('status' => array('gt' , -1)), $field = 'page_id,page_name', $order = 'sort desc,page_id desc'){
        return M('online_one_page')->field($field)->where($where)->order($order)->select();
    }
    
    /**
     * @todo    获取功能区详细信息
     * @author  fang
     */
    public function getOnePageInfo($id){
        if($id == 0) return null;
        return M('online_one_page')->field(true)->where(array('page_id' => $id, 'status' => 1))->find();
    }
    
    /**
     * @todo    获取我的收藏列表
     * @author  fang
     */
    public function getMyCollection($field = true){
        return M('online_my_collection')->field($field)->where(array('customer_id' => UID, 'activity_id' => $this->getOnlineID()))->order('collection_id desc')->select();
    }
    
    /**
     * @todo    添加收藏
     * @author  fang
     */
    public function collectionAdd($goods_id){
        if($goods_id == 0) return false;
        $goods_info = M('online_shop_goods')->field('online_shop_id,online_goods_name,online_goods_pic')->where(array('online_goods_id' => $goods_id))->find();
        $data = array(
            'online_shop_id' => (int)$goods_info['online_shop_id'],
            'online_goods_id' => (int)$goods_id,
            'online_goods_name' => (string)$goods_info['online_goods_name'],
            'online_goods_pic' => (int)$goods_info['online_goods_pic'],
            'customer_id' => UID,
            'activity_id' => $this->getOnlineID(),
            'create_time' => NOW_TIME,
        );
        $add = M('online_my_collection')->add($data);
        return ($add)? true : false ;
    }
    
    /**
     * @todo    删除收藏
     * @author  fang
     */
    public function collectionDel($id){
        if($id == 0) return false;
        $del = M('online_my_collection')->where(array('collection_id' => $id))->delete();
        return ($del)? true : false ;
    }
}
